﻿Pager = function(tableSource, divPager) {
	this.tableSource = tableSource; // bang nguon
	this.currentPage = 1; // trang hien tai - bat dau tu 1
	this.currentGroupPage = 1; // nhom page - 1 currentGroupPage = pageCount*currentPage
	this.minPage = 1; // trang dau tien
	this.maxPage = 1; // trang cuoi cung
	this.itemPerPage = 5; // so ban ghi tren moi trang
	this.pageCount = 3; // so luong trang dc hien thi 
	this.inited = false;
	this.divPager = divPager; // the div cho viec phan trang
	this.isHaveFooter = false; // hien footer ko?
	
	// css
	this.normalPageClass = 'normalPage';
	this.activePageClass = 'activePage';
	this.morePageClass = 'morePage';
	
	this.showAllRecords = function(iFrom, iTo)
	{     
        var rows = this.tableSource.rows;
        for (var i = 1; i < rows.length; i++) {
        	rows[i].style.display = '';
        }
	};
	
	this.showRecords = function(iFrom, iTo)
	{     
        var rows = this.tableSource.rows;
        for (var i = 1; i < rows.length; i++) {
            if (i < iFrom || i > iTo) rows[i].style.display = 'none';
            else rows[i].style.display = '';
        }
	};
	
	this.showPage = function(iPageNumber)
	{
        if (! this.inited) 
    	{
            alert("not inited");
            return;	
    	}
        this.currentGroupPage = Math.ceil(iPageNumber / this.pageCount);     
        
		var oldPageAnchor = document.getElementById('pg_attb_' + this.divPager.id + '_' + this.currentPage);
		if(oldPageAnchor != null) oldPageAnchor.className = this.normalPageClass;
		
		this.currentPage = iPageNumber;
		var newPageAnchor = document.getElementById('pg_attb_' + this.divPager.id + '_' + this.currentPage);
		if(newPageAnchor != null) newPageAnchor.className = this.activePageClass;
		
		var from = (iPageNumber - 1) * this.itemPerPage + 1;
		var to = from + this.itemPerPage - 1;
		this.showRecords(from, to);
		// hien row cuoi cung - neu co tfoot
		//if(this.isHaveFooter) this.tableSource.rows[this.tableSource.rows.length - 1].style.display = "";
		//else this.tableSource.rows[this.tableSource.rows.length - 1].style.display = "none";
	};
	
	this.morePage = function() {
		var aMorePage = $(this.divPager).find("." + this.morePageClass);
		this.currentGroupPage = this.currentGroupPage + 1;
		var itemMax = this.currentGroupPage * this.pageCount;
		if(itemMax >= this.maxPage) 
		{
			$(aMorePage).css('display', 'none');			
		}
		var itemStart = (this.currentGroupPage - 1)*this.pageCount;
		var itemEnd = this.maxPage;
		var i = 0;
		for(i=itemStart; i<itemMax; i++)
		{
			var itemOld = i - this.pageCount + 1;
			var indexPage = i + 1;
			var aPage = document.getElementById('pg_attb_' + this.divPager.id + '_' + itemOld);
			if(aPage != undefined)
			{
				// neu >= so luong max item thi an nut di 
				if(i >= this.maxPage)
				{
					aPage.id = 'pg_attb_' + this.divPager.id + '_' + indexPage;
					$(aPage).css('display', 'none');
				}
				else
				{
					aPage.id = 'pg_attb_' + this.divPager.id + '_' + indexPage;				
					$(aPage).text(indexPage);			
					$(aPage).attr('pageindex', indexPage);
					if(i == itemStart) 
					{
						$(aPage).addClass(this.activePageClass);
					}					
					else $(aPage).removeClass(this.activePageClass).addClass(this.normalPageClass);
				}
			}
		}
		this.showPage(itemStart + 1);
	};
	
	this.firstPage = function()
	{
		this.currentPage = this.pageCount + 1;
		this.currentGroupPage = 2; // vi khi thuc hien ham prevPage() thi se tu dong tru 1
		this.prevPage();
		this.showPage(1);
	};
	
	this.lastPage = function()
	{		
		this.currentGroupPage =  Math.ceil(this.maxPage / this.pageCount) - 1; // vi khi thuc hien ham morePage() thi se tu dong cong 1
		this.morePage();
		this.showPage(this.maxPage);
	};
	
	this.prevPage = function()
	{
		var sodu = 0;
		if(this.pageCount == 1) sodu = 1;
		else sodu = this.currentPage % this.pageCount;
		// thiet lap lai 1 group moiw - hien thi morepage button
		if(sodu == 1 && this.currentPage != this.minPage)
		{
			// hien thi morepage button
			var aMorePage = $(this.divPager).find("." + this.morePageClass);
			$(aMorePage).css('display', '');
			this.currentGroupPage = this.currentGroupPage - 1;
			var itemEnd = this.currentGroupPage*this.pageCount
			var itemStart = itemEnd - this.pageCount;
			var i = 0;
			var bHasItemOld = false; // kiem tra xem cos item o trang tiep theo ko
			for(i=itemStart; i<itemEnd; i++)
			{
				var itemOld = i + this.pageCount + 1;
				var indexPage = i + 1;
				var aPage = document.getElementById('pg_attb_' + this.divPager.id + '_' + itemOld);
				if(aPage != undefined)
				{
					bHasItemOld = true;
					aPage.id = 'pg_attb_' + this.divPager.id + '_' + indexPage;				
					$(aPage).text(indexPage);			
					$(aPage).attr('pageindex', indexPage);
					if($(aPage).css('display') == 'none') $(aPage).css('display', '');
					if(i == itemEnd) 
					{
						$(aPage).addClass(this.activePageClass);
					}					
					else $(aPage).removeClass(this.activePageClass).addClass(this.normalPageClass);
				}
			}
			if(!bHasItemOld)
			{
				for(i=itemStart; i<itemEnd; i++)
				{
					var aPage = document.getElementById('pg_attb_' + this.divPager.id + '_' + (i+1));
					$(aPage).removeClass(this.activePageClass);
				}
			}
			this.currentPage = itemEnd;
			this.showPage(this.currentPage);
		}
		else if (this.currentPage > this.minPage) this.showPage(this.currentPage - 1);        
	};
	
	this.nextPage = function()
	{
		var sodu = this.currentPage % this.pageCount;
		if(sodu == 0 && this.currentPage != this.maxPage) this.morePage();
		else if (this.currentPage < this.maxPage) this.showPage(this.currentPage + 1);
        
	};	           
   
    this.init = function(params) {
        var rows = this.tableSource.rows;
        var records = (rows.length - 1);
        this.minPage = 1;
        this.maxPage = Math.ceil(records / this.itemPerPage);
        this.inited = true;

		// tao phan trang
		var divPager = this.divPager;
		$(divPager).html('');
		//base.removeChildDiv(divPager);
		
		// trang dau tien
		var aFirst = document.createElement("a");
		aFirst.innerHTML = "&lt;&lt;";
		$(aFirst).addClass(this.normalPageClass);
		$(aFirst).css('cursor', 'pointer');
		aFirst.pager = this;
		$(aFirst).mousedown(function() {
			this.pager.firstPage();
		}); 
		// trang truoc 
		var aPrev = document.createElement("a");
		aPrev.innerHTML = "&lt;";
		$(aPrev).addClass(this.normalPageClass);
		$(aPrev).css('cursor', 'pointer');
		aPrev.pager = this;
		$(aPrev).mousedown(function() {
			this.pager.prevPage();
		});
		// trang ke tiep
		var aNext = document.createElement("a");
		aNext.innerHTML = "&gt;";
		$(aNext).addClass(this.normalPageClass);
		$(aNext).css('cursor', 'pointer');
		aNext.pager = this;
		$(aNext).mousedown(function() {
			this.pager.nextPage();
		});
		// trang cuoi cung
		var aLast = document.createElement("a");
		aLast.innerHTML = "&gt;&gt;";
		$(aLast).addClass(this.normalPageClass);
		$(aLast).css('cursor', 'pointer');
		aLast.pager = this;
		$(aLast).mousedown(function() {
			this.pager.lastPage();
		});
		// hien thi nhieu trang tiep theo
		var aMorePage = document.createElement("a");
		$(aMorePage).text("...");
		$(aMorePage).addClass(this.morePageClass);
		$(aMorePage).css('cursor', 'pointer');
		aMorePage.pager = this;
		$(aMorePage).mousedown(function() {
			this.pager.morePage();
		});
		
		//$(divPager).append(aFirst);
		$(divPager).append(aPrev);
		// cac trang hien tai
		var aPage = null;
		var i = 0;
		var pageCount = this.maxPage > this.pageCount ? this.pageCount : this.maxPage;		
		for(i=0; i<pageCount; i++)
		{
			var indexPage = i + 1;
			aPage = document.createElement("a");
			aPage.id = 'pg_attb_' + this.divPager.id + '_' + indexPage;
			$(aPage).text(indexPage);			
			$(aPage).attr('pageindex', indexPage);
			$(aPage).css('cursor', 'pointer');
			aPage.pager = this;
			if(i == 0) $(aPage).addClass(this.activePageClass);
			else $(aPage).addClass(this.normalPageClass);
			$(aPage).mousedown(function() {
				var indexPage = parseInt($(this).attr('pageindex'));
				this.pager.showPage(indexPage);
			});
			$(divPager).append(aPage);
		}
		if(this.maxPage > this.pageCount) $(divPager).append(aMorePage);
		$(divPager).append(aNext);
		//$(divPager).append(aLast);
		this.showPage(1);
    };
};